/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bt;

import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author kszyh
 */
public class Receiver {

    private BluetoothConnection[] btConnections;
    private boolean isServer;
    private Vector consoleStrings = new Vector();
    private Midlet midlet;

    public Receiver(BluetoothConnection[] btConns, Midlet _midlet, boolean _isServer) {
        midlet = _midlet;
        btConnections = btConns;
        isServer = _isServer;

        for (int i = 0; i < btConnections.length; i++) {
            ReceiveThread rt = new ReceiveThread(i);
            rt.start();
        }
    }

    private synchronized void close() {
        for (int i = 0; i < btConnections.length; i++) {
            btConnections[i].close();
        }
    }

    public BluetoothConnection getConnection(int id) {
        return btConnections[id];
    }

    public void updateRemoteChar(int id, String c) {
        if (true == isServer) {
            for (int i = 0; i < btConnections.length; i++) {
                sendIntegerToPeer(i, (String) c);
            //midlet.getReceivedString().setText("Wyslano: " + c);
            }
        }
        analyzeRecv(c);
    //midlet.getReceivedString().setText(c);
    }

    public void analyzeRecv(String s) {
        if (s.startsWith("^txt^") == true) { // to znaczy, że idzie text;
            int length = s.length();
            String showedS = s.substring(5, length);
            midlet.getReceivedString().setText(showedS);
            return;
        }
        if (s.startsWith("^img^") == true) { // to znaczy ze idzie obrazek
            int length = s.length();
            String cuted = s.substring(5, length);
            midlet.displayImage(cuted);
            return;
        }
        midlet.getReceivedString().setText("Nierozpoznany typ wiadomosci!");

    }

    private void sendIntegerToPeer(int peerIndex, String c) {
        BluetoothConnection connection = btConnections[peerIndex];
        try {
            connection.writeString(c); //wysyla ?
        } catch (IOException ex) {
            midlet.getReceivedString().setText("Bład! Zamykam połączenie.");
            connection.close();
        }
    }

    void sendString(String s) {
        if (true == isServer) {
            for (int i = 0; i < btConnections.length; i++) {
                sendIntegerToPeer(i, s);
            }
        } else {
            sendIntegerToPeer(0, s);
        //midlet.getReceivedString().setText("Wyslane!");
        }

    }

    private class ReceiveThread extends Thread {

        private int index;

        public ReceiveThread(int i) {
            index = i;
        }

        public void run() {
            String inp;
            while (true) {
                try {
                    inp = getConnection(index).readString();
                } catch (IOException e) {
                    System.out.println("Bład! Nie udało się uruchomić wątku.");
                    getConnection(index).close();
                    return;
                }

                /*if(inp == "X"){
                getConnection(index).close();
                return;
                }*/

                updateRemoteChar(index, inp);
            }
        }
    }
}
